package rs.frenjoynet.core.ui;

import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.In;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
import org.jboss.seam.contexts.Context;
import org.jboss.seam.contexts.Contexts;

import javax.faces.context.FacesContext;
import javax.faces.context.ExternalContext;
import javax.faces.application.FacesMessage;
import javax.servlet.http.HttpSession;
import java.util.ResourceBundle;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.text.MessageFormat;

import rs.frenjoynet.core.core.CoreSecurityFacade;
import rs.frenjoynet.core.core.CoreApplicationsFacade;
import rs.frenjoynet.core.core.LoginKind;
import rs.frenjoynet.core.core.ActiveUserSession;
import rs.frenjoynet.core.ui.session.UtilisCoreSessionBean;

/**
 * Created by IntelliJ IDEA.
 * User: ppopovic
 * Date: 08.10.2009.
 * Time: 11.14.16
 * To change this template use File | Settings | File Templates.
 */
public class BaseBean
{
	@SuppressWarnings ({"SeamAnnotationsInconsistencyInspection"})
	@In ("sessionBean")
	private UtilisCoreSessionBean utilisCoreSessionBean;

	private FacesMessages facesMessages;

	@SuppressWarnings ({"SeamAnnotationsInconsistencyInspection"})
	@Logger
	private Log log;

	private String SHOW_CONTENT = "showContent";

	protected CoreApplicationsFacade getCoreApplicationsFacade()
	{
		return utilisCoreSessionBean.getCoreApplicationsFacade();
	}

	protected CoreSecurityFacade getCoreSecurityFacade()
	{
		return utilisCoreSessionBean.getCoreSecurityFacade();
	}

	public FacesContext getFacesContext()
	{
		return FacesContext.getCurrentInstance();
	}

	public ExternalContext getExternalContext()
	{
		return getFacesContext().getExternalContext();
	}

	public HttpSession getHttpSession()
	{
		return (HttpSession)getFacesContext().getExternalContext().getSession(false);
	}

	public FacesMessages getFacesMessages()
	{
		if (facesMessages == null)
		{
			facesMessages = FacesMessages.instance();
		}
		return facesMessages;
	}

	private ResourceBundle getResourceBundle(String resource)
	{
		if (resource != null)
			return getFacesContext().getApplication().getResourceBundle(getFacesContext(), resource);
		else
			log.warn("resource " + resource + " is not found");
		return null;
	}

	protected String getResourceString(String resource, String key)
	{
		return getResourceBundle(resource).getString(key);
	}

	protected String getResourceString(String resource, String key, Object... params)
	{
		return MessageFormat.format(getResourceBundle(resource).getString(key), params);
	}

	public String getCoreResourceString()
	{
		return "";//Constants.UTILIS_CORE_LABELS_RESOURCE;
	}
//
//	public String getCoreFormatResourceString()
//	{
//		return Constants.UTILIS_CORE_FORMAT_RESOURCE;
//	}

	protected String getModalPanelHeaderString(String operation, String object)
	{
		return getResourceString(getCoreResourceString(), "crudPanelHeader", operation, object);
	}

	public String getModalPanelNewHeader(String object)
	{
		return getModalPanelHeaderString(getResourceString(getCoreResourceString(), "addLabel"), object);
	}

	public String getModalPanelEditHeader(String object)
	{
		return getModalPanelHeaderString(getResourceString(getCoreResourceString(), "editLabel"), object);
	}

	public String getModalPanelDeleteHeader(String object)
	{
		return getModalPanelHeaderString(getResourceString(getCoreResourceString(), "deleteLabel"), object);
	}

	public String getFormFieldLabel(String field)
	{
		return getResourceString(getCoreResourceString(), "formFieldLabel", field);
	}

	protected String getConfirmPanelTextString(String operation, String object, String name)
	{
		return getResourceString(getCoreResourceString(), "confirmPanelText", operation, object, name);
	}

	public boolean isError()
	{
		boolean error = getFacesContext().getMessages().hasNext();
		if (error)
		{
			Iterator<FacesMessage> iterator = getFacesContext().getMessages();
			while (iterator.hasNext())
			{
				FacesMessage message = iterator.next();
				log.error("message: " + message.getSummary() + "; " + message.getDetail());
			}
		}
		return error;
	}

	private String getKey()
	{
		return this.getClass().toString();
	}

	@SuppressWarnings ("unchecked")
	private Map<String, Boolean> getShowContentValues()
	{
		Context conversationContext = Contexts.getConversationContext();
		if (conversationContext == null)
			return null;

		return (Map<String, Boolean>)conversationContext.get(SHOW_CONTENT);
	}

	private void saveShowContentValue(String key, Boolean value)
	{
		Map<String, Boolean> map = getShowContentValues();
		if (map == null)
		{
			map = new HashMap<String, Boolean>();
		}
		map.put(key, value);
		Contexts.getConversationContext().set(SHOW_CONTENT, map);
	}

	public boolean isShowContent()
	{
		boolean result = false;
		Map<String, Boolean> map = getShowContentValues();
		if (map != null)
		{
			Boolean value = map.get(getKey());
			result = (value != null && value);
		}
		return result;
	}

	public void setShowContent(boolean showContent)
	{
		saveShowContentValue(getKey(), showContent);
	}

	public void hideModalPanels()
	{
		// you can override this method if it is necessary. Keep setShowContent(false);
		setShowContent(false);
	}

	public String getLogedPage()
	{
		if(utilisCoreSessionBean.getLoginKind()== LoginKind.Master_Administrator)
		{
			return "/admin.xhtml";
		}
		else
			return "/home.xhtml";
	}

	public void setLoginKind(LoginKind loginKind)
	{
		utilisCoreSessionBean.setLoginKind(loginKind);
	}

	protected ActiveUserSession getActiveUserSession()
	{
		return utilisCoreSessionBean.getCoreSecurityFacade().getActiveUserSession();
	}
}

